#! /bin/sh

if [ -z "$BTRACE_HOME" -o ! -d "$BTRACE_HOME" ] ; then
  # resolve links - $0 could be a link to btrace's home
  PRG="$0"
  progname=`basename "$0"`
  BTRACE_HOME=`dirname "$PRG"`/..
  BTRACE_HOME=`cd "$BTRACE_HOME" && pwd`
fi

if [ "${JAVA_HOME}" != "" ]; then
   case "`uname`" in
      Darwin*)
          # In Mac OS X, tools.jar is classes.jar and is kept in a 
          # different location. Check if we can locate classes.jar
          # based on ${JAVA_VERSION}
          TOOLS_JAR="/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Classes/classes.jar"

          # if we can't find, try relative path from ${JAVA_HOME}. Usually,
          # /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
          # is JAVA_HOME. (or whatever version beyond 1.6.0!)
          if [ ! -f ${TOOLS_JAR} ] ; then
              TOOLS_JAR="${JAVA_HOME}/../Classes/classes.jar" 
          fi

          # If we still can't find, tell the user to set JAVA_VERSION.
          # This way, we can avoid zip file errors from the agent side
          # and "connection refused" message from client.
          if [ ! -f ${TOOLS_JAR} ] ; then
              echo "Please set JAVA_VERSION to the target java version"
              exit 1
          fi
      ;;
      *)
          TOOLS_JAR="${JAVA_HOME}/lib/tools.jar"
      ;;
   esac
   ${JAVA_HOME}/bin/java -Dcom.sun.btrace.probeDescPath=. -Dcom.sun.btrace.dumpClasses=false -Dcom.sun.btrace.debug=false -Dcom.sun.btrace.unsafe=false -cp ${BTRACE_HOME}/lib/*:${TOOLS_JAR}:/usr/share/lib/java/dtrace.jar com.sun.btrace.client.Main $*
else
   echo "Please set JAVA_HOME before running this script"
   exit 1
fi
